<template>
	<view>
		<view class="header status_bar" :class="scrrol == true ? 'scrrolHeader' : ''">
			<view class="back iconfont icon-xiangzuo1" @tap="backPage"></view>
			<view class="text" :class="scrrol == true ? 'scrrolHeader' : ''">{{ Lang.lang('登錄') }}</view>
			<view class="back"></view>
			<!-- <view class="rule">规则</view> -->
		</view>
		<view class="clear_box"></view>

		<view class="logo">
			<image src="../../static/bg05.png" mode="widthFix"></image>
		</view>
		<view class="login_title">{{ Lang.lang('一起加入') }}</view>
		<view class="login_title">{{ Lang.lang('建設未來的行列') }}</view>
		<view class="login_text">{{ Lang.lang('管理您的數字交易賬戶') }}</view>

		<view class="confirm02" @click="login">{{ Lang.lang('登錄') }}</view>



	</view>
</template>



<script>
	import Web3 from 'web3';

	export default {
		data() {
			return {
				scrrol: false,
				langIndex: '',
				version: '1.0.0',
				lang: 'tc',
				//==============
				web3: null,
				agent_code: ''

			}
		},
		onLoad(options) {
			let lang = uni.getStorageSync('_language') || 'tc';
			uni.setStorageSync('_language', lang);
			uni.setTabBarItem(this.Lang.lang('indexMenu')[0]);
			uni.setTabBarItem(this.Lang.lang('indexMenu')[1]);
			uni.setTabBarItem(this.Lang.lang('indexMenu')[2]);
			uni.setTabBarItem(this.Lang.lang('indexMenu')[3]);

			//==============================
			this.agent_code = options.agent_code || '';
			if (window.ethereum) {
				this.web3 = new Web3(window.ethereum);
				try {
					let network = ethereum.networkVersion;
					if (network != 56) {
						this.Msg.Fail(this.Lang.lang('只能在幣安鏈使用'))
						return;
					}
				} catch (error) {

				}
			} else {
				this.Msg.Fail(this.Lang.lang('只能在幣安鏈使用'))
				return;
			}
		},
		onShow() {
			this.lang = uni.getStorageSync('_language');
		},
		methods: {
			choiceLang: function(e) {
				let that = this;
				that.langIndex = e.target.value
				let lang = that.Lang.lang('langData')[that.langIndex].type;
				uni.setStorageSync('_language', lang);
				location.reload()
			},

			login() {
				let that = this
				window.ethereum.enable().then(address => {
					that.web3.eth.getCoinbase((err, accounts) => {
						that.walletAddress = accounts
						that.Net._post('api/user/getNonce', {
							address: accounts
						}, res => {
							that.web3.eth.personal.sign(res.data.nonce, accounts).then(sign => {
								that.Net._post('api/user/login', {
									address: accounts,
									sign: sign,
									agent_code: that.agent_code
								}, res1 => {
									if (res1.code == 200) {
										that.Net._setToken(res1.data.token)
										that.Nav._to({
											url: '/pages/index/index',
											type: 2
										});
									}
								})
							})
						});
					});
				});

			},

		}
	}
</script>

<style>
	.header {
		background-color: transparent !important;
	}

	.header .text {
		text-align: center;
	}

	.scrrolHeader {
		width: 100%;
		background-color: #1A1A1A !important;

	}

	.logo {
		width: 100%;
	}

	.logo image {
		width: 100%;
	}

	.login_title {
		width: 100%;
		font-size: 8vw;
		color: #fff;
		font-weight: bold;
		text-align: center;
		margin-top: 3vw;
	}

	.login_text {
		width: 100%;
		font-size: 3.8vw;
		color: #999999;
		text-align: center;
		margin-top: 4vw;
	}
</style>